package br.com.system.authentication;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

@ManagedBean
@SessionScoped
public class AuthenticationBean {

	public static final String authenticationKeyName = "user.auth.key";
	private String usuario;
	private String senha;

	public AuthenticationBean() {

	}

	public boolean isLoggedIn() {
		return FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(authenticationKeyName) != null;
	}

	public String login() {
		FacesContext.getCurrentInstance().getExternalContext().getSessionMap()
				.put(authenticationKeyName, generateKey());
		return "logado";
	}

	public String logout() {
		FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove(authenticationKeyName);
		HttpSession session = ((HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(false));
		while(session != null) {
			session.invalidate();
			session = ((HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(false));
		}
		return "deslogado";
	}
	
	private String generateKey() {
		return "authorized";
	}

	public String getUsuario() {
		return usuario;
	}

	public void setUsuario(String usuario) {
		this.usuario = usuario;
	}

	public String getSenha() {
		return senha;
	}

	public void setSenha(String senha) {
		this.senha = senha;
	}

}
